import { Component, OnInit, ViewChild } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { secondSaleAfterStatusArrs } from '../used-after-sales-manage.pipe';
import { UsedAfterSalesManageService } from '../used-after-sales-manage.service';

@Component({
  selector: 'app-used-after-sales-list',
  templateUrl: './used-after-sales-list.component.html',
  styleUrls: ['./used-after-sales-list.component.scss']
})
export class UsedAfterSalesListComponent implements OnInit {
  @ViewChild('followUpRef') followUpRef: any;
  // 路由缓存恢复时
  static updateCache: any = null;

  searchOptions = [
    { type: 1, labelText: '售后单号', valueKey: 'returnNum', value: '' },
    { type: 1, labelText: '订单编号', valueKey: 'orderNum', value: '' },
    {
      type: 2,
      labelText: '售后状态',
      valueKey: 'state',
      value: [],
      multiple: true,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [...secondSaleAfterStatusArrs]
    },
    { type: 1, labelText: '物流单号', valueKey: 'expressNum', value: '' },
    { type: 1, labelText: '客户手机', valueKey: 'customerName', value: '', placeholder: '请输入手机号/姓名' },
    { type: 3, labelText: '申请时间', valueKey: ['createTimeStart', 'createTimeEnd'], value: [], isTimeSelect: true },
    { type: 3, labelText: '退款时间', valueKey: ['refundCreateTime', 'refundEndTime'], value: [], isTimeSelect: true }
  ];
  searchParanms: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  // 跟进 Modal
  initParams: any = {
    type: 11, // type  1、订单 2、售后 3、客户 4、出库 5、直降 6、退税 7、机器 8、商品
    modelId: 2, // 类型 1、旧的表示二手回收 2、表示二手商城 3、表示二手回收
    relationInformation: ''
  };

  constructor(
    private message: NzMessageService,
    private services: UsedAfterSalesManageService
  ) { }

  ngOnInit(): void {
    UsedAfterSalesListComponent.updateCache = () => {
      this.loadTable();
    }
  }

  async loadTable() {
    let params = {
      ...this.searchParanms,
      current: this.tableParams.page,
      size: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.getSecondAfterSaleList(params);
    this.tableParams.loading = false;
    
    if (code === 0) {
      this.tableParams.tableData = data?.records || [];
      this.tableParams.total = data.total || 0;
    } else {
      this.message.error(message);
    }
  }

  queryResetData(params: any) {
    this.searchParanms = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }

  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  // 跟进
  followModal(item: any) {
    this.initParams.relationInformation = item.returnNum;
    this.followUpRef.isModelVisible = true;
  }
}
